import {
    getPageSysMenu,
    getListSysMenu,
    addSysMenu,
    updateSysMenu,
    deleteSysMenu,
    detailMenu
} from '@/services/setting/sysMenuManage';

export default {
    state: {
        data: [],
        pagination: {
            pageSize: 10,
            current: 1,
            total: 0
        },
        sysMenu: {
            id: null,
            createUser: null,
            createTime: null,
            menuName: null,
            parentId: null,
            orderNum: null,
            url: null,
            visible: null,
            type: null,
            permission: null,
            basePermissoin: null,
            international: null,
            remark: null
        },
        sysMenuParam: {
            menuName: null,
            ids: null,
            parentId: null,
            parentIds: null
        },
        parentMenu: {}
    },
    effects: {
        *fetchPageMenu({ payload }, { put, call }) {
            const result = yield call(getPageSysMenu, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'setSysMenu',
                    payload: result.data
                });
                yield put({
                    type: 'setSysPagination',
                    payload
                });
            }
        },
        *fetchListMenu({ payload }, { put, call }) {
            const result = yield call(getListSysMenu, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'setParentMenu',
                    payload: result
                });
            }
        },
        *fetchAdd({ payload, callback }, { put, call }) {
            const result = yield call(addSysMenu, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'setSysMenuParam',
                    payload: result.data
                });
            }
            if (callback) callback();
        },
        *fetchUpdate({ payload, callback }, { put, call }) {
            const result = yield call(updateSysMenu, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'setSysMenuParam',
                    payload: result.data
                });
            }
            if (callback) callback();
        },
        *fetchDelete({ payload }, { put, call }) {
            const result = yield call(deleteSysMenu, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'setSysMenuParam',
                    payload: result.data
                });
            }
        },
        *fetchDetail({ payload, callback }, { put, call }) {
            const result = yield call(detailMenu, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'setSysMenu',
                    payload: result.data
                });
            }
            // eslint-disable-next-line no-unused-expressions
            if (callback) callback();
        }
    },
    reducers: {
        setSysMenu(state, action) {
            return {
                ...state,
                sysMenu: { ...action.payload }
            };
        },
        setParentMenu(state, action) {
            return {
                ...state,
                parentMenu: { ...action.payload }
            };
        },
        setSysMenuParam(state, action) {
            return {
                ...state,
                sysMenuParam: { ...action.payload }
            };
        },
        setSysPagination(state, action) {
            return {
                ...state,
                pagination: { ...action.payload }
            };
        }
    }
};
